/*

BlackRabbit - OpenSource IDE.
@author Edward Sarkisyan (edward.sarkisyan@gmail.com)
Use LGPL (http://www.gnu.org/copyleft/lesser.html)

*/

#include "pluginsview.h"
#include "pluginscrollareawidget.h"
#include "ui_pluginsview.h"

PluginsView::PluginsView(QWidget *parent, QVector<Plugin*> *plugins) :
    QDialog(parent),
    m_ui(new Ui::PluginsView)
{
    m_ui->setupUi(this);
    if(plugins !=0)
    {
        foreach(Plugin* pluginInst, *plugins)
        {
            PluginScrollAreaWidget *newWidget = new PluginScrollAreaWidget(this,pluginInst);
            connect(newWidget,SIGNAL(addBttnClicked(Plugin*)), this, SLOT(returnPlugin(Plugin*)));
            connect(newWidget, SIGNAL(addBttnClicked(Plugin*)), this, SLOT(close()));
            newWidget->setPluginIcon(pluginInst->icon(40, 40));
            widgets.push_back(newWidget);
            m_ui->pluginsContainerLayout->insertWidget(0,newWidget);
        }
    }
    m_returnPlugin = 0;
}

PluginsView::~PluginsView()
{
    delete m_ui;
}

void PluginsView::viewCollection()
{
    for(int i=0;i < m_ui->pluginsContainerLayout->count();i++)
    {
        foreach(PluginScrollAreaWidget* widget, widgets)
        {
            widget->setAddButtonEnabled(false);
        }
    }
    this->exec();
}
Plugin* PluginsView::getPlugin()
{
    m_returnPlugin = 0;
    this->exec();
    return m_returnPlugin;
}

QWidget* PluginsView::getPreviewWidget()
{

    return getPlugin()->widgetFor(WIDGET);
}

void PluginsView::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void PluginsView::returnPlugin(Plugin* senderPlugin)
{
    m_returnPlugin = senderPlugin;
}
